<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="选择人员" prop="username" :rules="rules.validateRequire">
            <selectUser ref="selectUser" v-model="model.username" :userId="model.userid" :initDisplayData.sync="model.username" :roleCode="roleCode" :roleId="roleId" @confirm="initUser"></selectUser>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="人员类型" prop="usertype" :rules="rules.validateRequire">
            <el-select v-model="model.usertype" filterable clearable>
              <el-option v-for="item in codeModel.usertypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="范围类型" prop="scopetype" :rules="rules.validateRequire">
            <el-select v-model="model.scopetype" filterable clearable>
              <el-option v-for="item in codeModel.scopetypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="model.scopetype === '1'">
          <el-form-item label="范围省份" prop="dispscopedept" :rules="rules.validateRequire">
            <el-select v-model="model.dispscopedept" filterable clearable multiple>
              <el-option v-for="item in codeModel.scopedeptList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="排序码" prop="sortcode">
            <el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效" prop="enabled">
            <el-radio-group v-model="model.enabled">
              <el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSelectUserIds" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import selectUser from './selectUser'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { selectUser },
  data() {
    return {
      moduleTitle: '流程人员',
      urlPrefix: '/base/flowRoleUser/',
      url: {
        // 额外的url
        saveUserRole: '/base/flowRoleUser/batchSave',
        scopedeptUpdate: '/base/flowRoleUser/scopedeptUpdate'
      },
      userData: []
    }
  },
  props: {
    roleCode: {
      type: String,
      default: ''
    },
    roleId: {
      type: Number,
      default: 0
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    initUser(paras) {
      this.userData = paras
      let name = ''
      paras.forEach((element) => {
        name += element.name
        name += ' '
      })
      this.$set(this.model, 'username', name)
    },
    /** 得到参照选择的用户*/
    doSelectUserIds() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.saveLoading = true
            // 做提交
            let paraMap = this.model
            paraMap.scopedeptList = !this.model.dispscopedept ? '' : this.model.dispscopedept.join(',')
            paraMap.userList = this.userData
            paraMap.rolecode = this.roleCode
            paraMap.roleid = this.roleId
            paraMap.selectuserids = this.$refs.selectUser.selectUserIds
            paraMap.uscopetype = this.model.scopetype
            postAction(this.url.saveUserRole, paraMap)
              .then((res) => {
                if (res.success) {
                  this.$refs.selectUser.selectUserIds = ''
                  this.$tap.message(res.message, 'success')
                  this.$refs.selectUser.visible = false
                  this.$parent.$refs.editForm.visible = false
                  this.$parent.doQuery()
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
                this.saveLoading = false
              })
              .catch((err) => {
                this.saveLoading = false
              })
          } else {
            this.saveLoading = true
            let paraMap = {}
            let updateData = {}
            paraMap = this.model
            updateData.scopedeptList = !this.model.dispscopedept ? '' : this.model.dispscopedept.join(',')
            updateData.userList = this.userData
            updateData.rolecode = this.roleCode
            updateData.roleid = this.roleId
            updateData.selectuserids = this.$refs.selectUser.selectUserIds
            updateData.uscopetype = this.model.scopetype
            updateData.sortcode = this.model.sortcode
            updateData.enabled = this.model.enabled
            updateData.usertype = this.model.usertype
            paraMap.updateData = updateData
            postAction(this.url.scopedeptUpdate, paraMap)
              .then((res) => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.$refs.selectUser.selectUserIds = ''
                  this.$refs.selectUser.visible = false
                  this.$parent.$refs.editForm.visible = false
                  this.$parent.doQuery()
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
                this.saveLoading = false
              })
              .catch((err) => {
                this.saveLoading = false
              })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
